﻿using System.Linq;
using System.Threading;
using ff14bot;
using ff14bot.Behavior;
using ff14bot.Helpers;
using ff14bot.Managers;
using TreeSharp;
using Action = TreeSharp.Action;

namespace Magitek.Helpers
{
    public static class Items
    {
        public static Composite Use(string itemname, bool wait)
        {
            return new Decorator(r => Magitek.WindowSettings.usefood && CanUseItem(Magitek.WindowSettings.foodname) && !Core.Player.HasAura("Well Fed") && !Core.Player.InCombat && !Core.Player.IsMounted && Core.Player.IsValid, new Sequence(
                    new Action(r =>
                            {
                                foreach (BagSlot a in InventoryManager.Slots)
                                {
                                    if (a.Name == itemname)
                                    {
                                        a.UseItem();
                                        Logging.Write("[Magitek] Using Item: " + a.Name);
                                        return;

                                    }
                                }
                            }),
                            new Decorator(r => wait, new Sleep(4000))));
        }

        public static bool CanUseItem(string itemname)
        {
            return InventoryManager.Slots.Any(u => u.Name == itemname);
        }
    }
}